package com.ruoyi.repn.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.repn.utils.HttpUtils;
import com.ruoyi.repn.utils.MD5Utils;
import com.ruoyi.repn.domain.ReportInfo;
import com.ruoyi.repn.domain.dto.EnterpriseDto;
import com.ruoyi.repn.domain.params.EnterpriseInfoParams;
import com.ruoyi.repn.service.ThirdPartyService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cpf
 * @date 2024/3/4 13:58
 * @description 第三方接口实习
 */
@Service
public class ThirdPartyServiceImpl implements ThirdPartyService {


    @Value("${shumai.appid}")
    private String appId;
    @Value("${shumai.appSecurity}")
    private String appSecurity;

    /**
     * 工商信息模糊查询
     * @param enterpriseInfoParams 查询参数
     * @return
     */
    @Override
    public AjaxResult getEnterpriseInfo(EnterpriseInfoParams enterpriseInfoParams) {
        String timestamp = System.currentTimeMillis() + "";
        //参数
        String keyword = enterpriseInfoParams.getKeyword();
        String pageNo= enterpriseInfoParams.getPageNo();
        String pageSize= enterpriseInfoParams.getPageSize();

        //接口地址
        String url = "https://api.shumaidata.com/v4/business3/get";

        String sign = MD5Utils.encrypt(appId+"&"+timestamp+"&"+appSecurity);

        Map<String,Object> params = new HashMap<>();
        params.put("appid",appId);
        params.put("timestamp",timestamp);
        params.put("sign",sign);
        params.put("keyword",keyword);
        params.put("pageNo",pageNo);
        params.put("pageSize",pageSize);
        String result = HttpUtils.get(url,null,params);

        // 解析整个JSON字符串
        JSONObject jsonObject = JSON.parseObject(result);

        if (jsonObject == null){
            throw new RuntimeException("查询失败");
        }
        //获取返回信息
        boolean success = (boolean) jsonObject.get("success");
        if (!success){
          throw new RuntimeException("查询失败");
        }

        // 获取嵌套在"data"内的"data"字段对应的JSONArray
        JSONArray dataArray = jsonObject.getJSONObject("data").getJSONArray("data");

        //将JSONArray转换为List<RepReport>
        List<EnterpriseDto> list = JSONArray.parseArray(dataArray.toJSONString(), EnterpriseDto.class);

        return AjaxResult.success(list);


    }

    /**
     * 验证手机号和身份证
     * @param report
     * @return
     */
    @Override
    public AjaxResult isPhoneAndCard(ReportInfo report) {
        if (!checkPhoneNumber(report.getPhoneNumber())){
            return AjaxResult.error("请输入正确的手机号");
        }
        if (!idCardCheck(report)){
            return AjaxResult.error("请输入正确的身份证号");
        }
        return AjaxResult.success("验证成功");
    }


    /**
     * 验证手机号
     * @param phoneNumber 手机号
     * @return boolean
     */
    public boolean checkPhoneNumber(String phoneNumber){
        String timestamp = System.currentTimeMillis() + "";
        //接口地址
        String url = "https://api.shumaidata.com/v1/mobile_status/check";
        String sign = MD5Utils.encrypt(appId+"&"+timestamp+"&"+appSecurity);
        System.out.println("sign: "+sign);

        Map<String,Object> params = new HashMap<>();
        params.put("appid",appId);
        params.put("timestamp",timestamp);
        params.put("sign",sign);
        params.put("mobile", phoneNumber);
        String result = HttpUtils.get(url,null,params);

        // 解析整个JSON字符串
        JSONObject jsonObject = JSON.parseObject(result);

        if (jsonObject == null){
            throw new RuntimeException("查询失败");
        }
        //获取返回信息
        boolean success = (boolean) jsonObject.get("success");
        if (!success){
            //错误
            return false;
        }
        JSONObject data = jsonObject.getJSONObject("data");
        System.out.println("手机" + data);
        if (data == null){
            return false;
        }
        return data.getInteger("status") == 0;

    }


    /**
     * 实名校验
     * @param report 用户信息
     * @return 是否成功
     */
    public boolean idCardCheck(ReportInfo report){
        String timestamp = System.currentTimeMillis() + "";
        //参数
        String name = report.getName();
        String idcard = report.getIdNumber();
        //接口地址
        String url = "https://api.shumaidata.com/v4/id_card/check";
        //MD5Utils文件请联系工作人员索取
        String sign = MD5Utils.encrypt(appId+"&"+timestamp+"&"+appSecurity);
        System.out.println("sign: "+sign);

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("appid",appId);
        params.put("timestamp",timestamp);
        params.put("sign",sign);
        params.put("name", name);
        params.put("idcard", idcard);
        //HttpUtils文件请联系工作人员索取
        String result = HttpUtils.get(url,null,params);
        System.out.println(result);

        // 解析整个JSON字符串
        JSONObject jsonObject = JSON.parseObject(result);

        if (jsonObject == null){
            throw new RuntimeException("查询失败");
        }
        //获取返回信息
        boolean success = (boolean) jsonObject.get("success");
        if (!success){
            //错误
            return false;
        }
        JSONObject data = jsonObject.getJSONObject("data");
        System.out.println("身份证" +data);
        if (data == null) {
            //错误
            return false;
        }
        //错误
        return data.getInteger("result") == 0;

    }
}
